<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-01-13 11:41:05
 * @Last Modified time: 2023-01-13 11:42:26
 */


namespace app\sys\controller\user;

use app\sys\controller\Base;
use think\facade\{
    App
};
use app\common\services\user\UserGroupService;
/**
 * 用户-分组
 * Class UserGroup 
 * @package app\sys\controller\user
 */
class UserGroup extends Base
{
    /**
     * UserGroup constructor.
     * @param App $app
     */
    public function __construct(App $app,UserGroupService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 分组列表
     * @method(GET)
     */
    public function list()
    {
        $where = $this->request->getMore([
        ]);
        $this->success($this->service->getUserGroupList($where));
    }

    /**
     * 分组数据
     * @noAuth(true)
     * @method(GET)
     */
    public function select()
    {
        $this->success($this->service->getUserGroupSelect());
    }

    /**
     * 分组详细 
     * @noAuth(true)
     * @method(GET)
     */
    public function detail(){
        $this->success($this->service->getUserGroupDetail($this->request->get('id')));
    }


    /**
     * 添加分组
     * @method(POST)
     */
    public function create()
    {
        $data = $this->request->postMore([
            'group_name',
        ]);
        if (!$data['group_name']) $this->error('请输入分组名称！');
        if($this->service->save( $data)){
            $this->success('添加成功!');
        }
        $this->error('添加失败!');
    }

    /**
     * 编辑分组
     * @method(PUT)
     */
    public function update()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'group_name',
        ]);
        if (!$data['group_name']) $this->error('请输入分组名称！');
        if($this->service->update($id, $data)){
            $this->success('修改成功!');
        }
        $this->error('修改失败!');
    }

    /**
     * 删除分组
     * @method(DELETE)
     */
    public function delete()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }

        if ($this->service->delete((int)$id)) {
            $this->success('删除成功!');
        } 
        $this->error('删除失败!');
    }

}